#!/bin/sh
### BEGIN INIT INFO
# Provides:          S30APP
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start EC20_Auto_Connect, KLD_JK_002 and load drivers
# Description:       Custom application services with driver loading
### END INIT INFO

# 驱动模块配置
DRIVER1="/mnt/ElectricityCollectionDyl.ko"
DRIVER2="/mnt/externalswitchdyl.ko"
DRIVER3="/mnt/SwitchInputDyl.ko"

# 应用程序配置
EC20_SCRIPT="/mnt/EC20_Auto_Connect.sh"
KLD_APP="/mnt/KLD_JK_002"
EC20_PIDFILE="/var/run/ec20_auto_connect.pid"
KLD_PIDFILE="/var/run/kld_jk_002.pid"

# 确保pid文件目录存在
mkdir -p $(dirname $EC20_PIDFILE) $(dirname $KLD_PIDFILE)

load_drivers() {
    echo "Loading kernel modules..."
    
    # 使用insmod加载指定路径的驱动模块
    insmod $DRIVER1 || return 1
    insmod $DRIVER2 || return 1
    insmod $DRIVER3 || return 1
    
    echo "Drivers loaded successfully"
}

unload_drivers() {
    echo "Unloading kernel modules..."
    
    # 获取驱动模块名（不包含路径和扩展名）
    MODULE1=$(basename $DRIVER1 .ko)
    MODULE2=$(basename $DRIVER2 .ko)
    MODULE3=$(basename $DRIVER3 .ko)
    
    # 卸载驱动模块（顺序与加载相反）
    rmmod $MODULE3 2>/dev/null
    rmmod $MODULE2 2>/dev/null
    rmmod $MODULE1 2>/dev/null
}

case "$1" in
    start)
        echo "Starting S60app services..."
        
        # 加载驱动
        if ! load_drivers; then
            echo "Failed to load drivers! Aborting..."
            exit 1
        fi
        
        sleep 1
        
        # 启动EC20自动连接脚本
        if [ -x "$EC20_SCRIPT" ]; then
            $EC20_SCRIPT &
            echo $! > $EC20_PIDFILE
            echo "EC20 Auto Connect started with PID $!"
        else
            echo "Warning: $EC20_SCRIPT not executable or not found"
        fi
        
        sleep 1

        # 启动KLD_JK_002应用
        if [ -x "$KLD_APP" ]; then
            $KLD_APP &
            echo $! > $KLD_PIDFILE
            echo "KLD_JK_002 started with PID $!"
        else
            echo "Warning: $KLD_APP not executable or not found"
        fi
        ;;
        
    stop)
        echo "Stopping S30APP services..."
        
        # 停止应用程序
        if [ -f "$EC20_PIDFILE" ]; then
            kill -9 $(cat $EC20_PIDFILE) 2>/dev/null
            rm -f $EC20_PIDFILE
            echo "EC20 Auto Connect stopped"
        fi
        
        if [ -f "$KLD_PIDFILE" ]; then
            kill -9 $(cat $KLD_PIDFILE) 2>/dev/null
            rm -f $KLD_PIDFILE
            echo "KLD_JK_002 stopped"
        fi
        
        # 卸载驱动
        unload_drivers
        ;;
        
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
        
    status)
        echo "Checking status..."
        
        # 检查驱动状态
        echo "Loaded modules:"
        lsmod | grep -e "$(basename $DRIVER1 .ko)" -e "$(basename $DRIVER2 .ko)" -e "$(basename $DRIVER3 .ko)"
        
        # 检查应用程序状态
        if [ -f "$EC20_PIDFILE" ]; then
            echo "EC20 Auto Connect is running with PID $(cat $EC20_PIDFILE)"
        else
            echo "EC20 Auto Connect is not running"
        fi
        
        if [ -f "$KLD_PIDFILE" ]; then
            echo "KLD_JK_002 is running with PID $(cat $KLD_PIDFILE)"
        else
            echo "KLD_JK_002 is not running"
        fi
        ;;
        
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0